package com.homedo.odin.solution.service;

import com.homedo.core.common.util.NumberHelper;
import com.homedo.core.common.util.ShortUrlHelper;
import com.homedo.odin.solution.config.SysConfigHelper;
import com.homedo.odin.solution.model.dto.PcAccountInfo;
import com.homedo.odin.solution.model.dto.ShortMessageBean;
import com.homedo.odin.solution.model.entity.Solution;
import com.homedo.odin.solution.service.common.HomedoService;
import java.math.BigDecimal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author: zhaoxudong
 * @date: 2018/4/9 16:54
 * @Description:
 */
@Component
public class SmsServiceImpl {

    @Autowired
    HomedoService homedoService;
    @Autowired
    SysConfigHelper sysConfigHelper;

    /**
     * 发方案被浏览短信
     */
    public void sendVisitSolutionSMS(Solution solution) {
        if (NumberHelper.validatePositive(solution.getUserId()) &&
                (solution.getPageView() + 1) % 100 == 0) {
            ShortMessageBean shortMessageBean = new ShortMessageBean();
            PcAccountInfo userMessageInfo = homedoService.getUserMessageInfo(solution.getUserId());
            String phone = null == userMessageInfo ? "" : userMessageInfo.getMobile();
            //String url = "http://172.16.220.152:3001/solutions/detail/"+SOLUTION.getSolutionId().toString()+".html";//测试环境
//            String url = "http://msolution.homedo.com/solutions/detail/" + SOLUTION.getSolutionId().toString() + ".html";//正式环境
            //M站方案馆详情地址
            String mUrl = sysConfigHelper.getMsolPageUrl().replace("@solutionId@", solution.getSolutionId().toString());
            int count = solution.getPageView() + 1;
            String content = "【河姆渡】尊敬的" + solution.getUserName() + "，您的方案“" + solution.getSolutionName() + "”已获得了"
                    + count + "次访问，详情请点击" + ShortUrlHelper.convertToSINAShortUrl(mUrl);
            shortMessageBean.setAccountId(solution.getUserId());
            shortMessageBean.setPhone(phone);
            shortMessageBean.setContent(content);
            homedoService.sendShortMessage(shortMessageBean);
        }
    }

    /**
     * 发方案被下载短信
     */
    public void sendDownloadSolutionSMS(Solution solution) {
        ShortMessageBean shortMessageBean = new ShortMessageBean();
        PcAccountInfo userMessageInfo = homedoService.getUserMessageInfo(solution.getUserId());
        String phone = null == userMessageInfo ? "" : userMessageInfo.getMobile();
        //String url = "http://172.16.220.152:3001/solutions/detail/"+SOLUTION.getSolutionId().toString()+".html";//测试环境
//        String url = "http://msolution.homedo.com/solutions/detail/"+SOLUTION.getSolutionId().toString()+".html";//正式环境
        //M站方案馆详情地址
        String mUrl = sysConfigHelper.getMsolPageUrl().replace("@solutionId@", solution.getSolutionId().toString());
        String content =
                "【河姆渡】尊敬的" + solution.getUserName() + "，您的方案“" + solution.getSolutionName() + "”刚刚发生了一笔订单支付，获得了"
                        + solution
                        .getCharges().multiply(new BigDecimal(50)).intValue()
                        + "个河币收益，请至”方案馆-我的账户“中查询，方案详情请点击" + ShortUrlHelper.convertToSINAShortUrl(mUrl);
        shortMessageBean.setAccountId(solution.getUserId());
        shortMessageBean.setPhone(phone);
        shortMessageBean.setContent(content);
        homedoService.sendShortMessage(shortMessageBean);
    }
}
